package wiki.zimo.certificate_manage.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import wiki.zimo.certificate_manage.constant.Constant;
import wiki.zimo.certificate_manage.dao.PrizeLevelDao;
import wiki.zimo.certificate_manage.domain.PrizeLevel;
import wiki.zimo.certificate_manage.service.ExcelService;
import wiki.zimo.certificate_manage.utils.UUIDUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 子墨
 * @date 2018/10/23 0:09
 * @describe 处理获奖级别
 */
@Controller
@RequestMapping("/prizeLevel")
public class PrizeLevelController {

    @Autowired
    private PrizeLevelDao prizeLevelDao;
    @Autowired
    private ExcelService excelService;

    /**
     * 新增页
     * @author 子墨
     * @date 2018/10/19 10:37
     * @params []
     * @return java.lang.String
     */
    @RequestMapping(value = "add")
    public String add(){
        return "prize/level/add";
    }

    /**
     * 列表页
     * @author 子墨
     * @date 2018/10/19 10:38
     * @params []
     * @return java.lang.String
     */
    @RequestMapping(value = "list")
    public String list(){
        return "prize/level/list";
    }

    /**
     * 更新页
     * @author 子墨
     * @date 2018/10/19 15:03
     * @params [map, id]
     * @return java.lang.String
     */
    @RequestMapping(value = "update")
    public String update(Map<String,Object> map,
                         @RequestParam(value = "id",required = true)String id){
        PrizeLevel prizeLevel = prizeLevelDao.findPrizeLevelById(id);
        map.put("prizeLevel",prizeLevel);
        return add();
    }

    /**
     * 一键导出Excel数据
     * @author 子墨
     * @date 2018/10/19 15:09
     * @params [response]
     * @return void
     */
    @RequestMapping(value = "/export")
    public void exportExcel(HttpServletResponse response) {
        List<PrizeLevel> list = prizeLevelDao.findAll();
        System.out.println(list.toString());
        // 指定列表标题和工作表名称
        ExportParams params = new ExportParams("获奖级别信息","获奖级别信息表");
        try {
            excelService.doExportExcel(response,list,PrizeLevel.class,params,"获奖级别信息表");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 新增或者修改获奖级别信息接口
     * @author 子墨
     * @date 2018/10/18 22:53
     * @params [map, id, schoolName]
     * @return wiki.zimo.certificate_manage.domain.PrizeLevel
     */
    @RequestMapping(value = "/addPrizeLevel")
    @ResponseBody
    public PrizeLevel addPrizeLevel(Map<String,Object> map,
                                    @RequestParam(value = "id",required = false)String id,
                                    @RequestParam(value = "prizeLevel",required = true) String level){
        if (id == null || id.replaceAll(" ","").equals("")){
            PrizeLevel prizeLevel = new PrizeLevel();
            prizeLevel.setId(UUIDUtils.getRandomID());
            prizeLevel.setPrizeLevel(level);
            return prizeLevelDao.save(prizeLevel);
        }
        PrizeLevel prizeLevel = prizeLevelDao.findPrizeLevelById(id);
        prizeLevel.setPrizeLevel(level);
        return prizeLevelDao.save(prizeLevel);
    }

    /**
     * 得到所有获奖级别信息接口
     * @author 子墨
     * @date 2018/10/19 12:38
     * @params []
     * @return java.util.List<wiki.zimo.certificate_manage.domain.PrizeLevel>
     */
    @RequestMapping(value = "/getAllPrizeLevel")
    @ResponseBody
    public List<PrizeLevel> getAllPrizeLevel(){
        return prizeLevelDao.findAll();
    }

    /**
     * 删除获奖级别信息接口
     * @author 子墨
     * @date 2018/10/19 14:11
     * @params [id]
     * @return java.lang.String
     */
    @RequestMapping(value = "delete")
    public String delete(Map<String,Object> map,
                         @RequestParam(value = "id")String id){
        if (prizeLevelDao.existsById(id)){
            prizeLevelDao.deleteById(id);
        }
        map.put(Constant.MSG,"提示：删除成功");
        return list();
    }
}
